package com.ciwong.epaper.common.animdialogactivity;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.me.bean.MedalsBean;
import com.ciwong.epaper.util.EConstants;
import com.ciwong.epaper.util.ViewUtil;
import com.ciwong.libs.utils.CWLog;
import com.ciwong.mobilelib.bean.UserInfoBase;
import com.facebook.drawee.view.SimpleDraweeView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

public class ViewInflater {
    /**
     * 回调接口
     *
     * @author res-jxliu
     */
    public interface CallBack {
        /**
         * 回调方法
         */
        void callback(boolean makeBitmapSuccess, View view);
    }

    public static View getViewWithData(Context context, MedalsBean medalsBean, UserInfoBase userInfoBase, CallBack callBack) {
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        View inflate = LayoutInflater.from(context).inflate(R.layout.dialog_integral_view, null);

        View ll_share_view = inflate.findViewById(R.id.ll_share_view);

        ll_share_view.measure(View.MeasureSpec.makeMeasureSpec(dm.widthPixels, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(dm.heightPixels, View.MeasureSpec.EXACTLY));
        ll_share_view.layout(0, 0, ll_share_view.getMeasuredWidth(), ll_share_view.getMeasuredHeight());
        SimpleDraweeView iv_dialog_header = (SimpleDraweeView) ll_share_view.findViewById(R.id.iv_dialog_header);
        ImageView iv_dialog_medal = ll_share_view.findViewById(R.id.iv_dialog_medal);
        ImageView iv_qr_code = ll_share_view.findViewById(R.id.iv_qr_code);
        RatingBar ratingBar_rarity = ll_share_view.findViewById(R.id.ratingBar_rarity);
        TextView tv_user_name = ll_share_view.findViewById(R.id.tv_user_name);
//        TextView tv_des_congratulate = ll_share_view.findViewById(R.id.tv_des_congratulate);
        TextView tv_medal_name = ll_share_view.findViewById(R.id.tv_medal_name);
//        TextView tv_rarity = ll_share_view.findViewById(R.id.tv_rarity);
        TextView tv_des_content = ll_share_view.findViewById(R.id.tv_des_content);

        refrashtextviewdate(userInfoBase, medalsBean, tv_user_name, tv_medal_name, tv_des_content, ratingBar_rarity);
        refrashImageViewdate(userInfoBase, medalsBean, iv_dialog_header, iv_dialog_medal, callBack, ll_share_view);

        return ll_share_view;
    }


    private static void refrashtextviewdate(UserInfoBase userInfoBase, MedalsBean medalsBean,
                                            TextView tv_user_name, TextView tv_medal_name, TextView tv_des_content, RatingBar ratingBar_rarity) {

        if (userInfoBase == null)
            userInfoBase = EApplication.getInstance().getUserInfoBase();
        tv_user_name.setText(userInfoBase.getRealName());

        if (medalsBean == null) return;
        tv_medal_name.setText(medalsBean.getMedalName());
        ratingBar_rarity.setRating(medalsBean.getRarity());
        tv_des_content.setText(medalsBean.getRemark());
    }

    private static void refrashImageViewdate(UserInfoBase userInfoBase, MedalsBean medalsBean, SimpleDraweeView iv_dialog_header, final ImageView iv_dialog_medal, final CallBack callBack, final View inflate) {

        if (userInfoBase == null)
            userInfoBase = EApplication.getInstance().getUserInfoBase();
        if (!TextUtils.isEmpty(userInfoBase.getAvatar())) {
            Uri uri = Uri.parse(userInfoBase.getAvatar());
            iv_dialog_header.setImageURI(uri);
        } else {
            ViewUtil.setLocalDefaultImg(iv_dialog_header, R.mipmap.head);
        }
        ImageLoader.getInstance().displayImage(medalsBean.getIcon() + "?x-oss-process=style/square_420", iv_dialog_medal, EConstants.getDefaultpictureOption(R.mipmap.icon_medal_big), new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String s, View view) {
                CWLog.e("lzh ImageLoader: ", " onLoadingStarted " + s);
            }

            @Override
            public void onLoadingFailed(String s, View view, FailReason failReason) {
                CWLog.e("lzh ImageLoader: ", " onLoadingFailed " + failReason);
                if (callBack != null) {
                    callBack.callback(false, inflate);
                }

            }

            @Override
            public void onLoadingComplete(String s, View view, Bitmap bitmap) {
                CWLog.e("lzh ImageLoader: ", " onLoadingComplete " + s);
                CWLog.e("lzh ImageLoader: ", " onLoadingComplete " + bitmap.getByteCount());
                if (callBack != null) {
                    callBack.callback(true, inflate);
                }
            }

            @Override
            public void onLoadingCancelled(String s, View view) {
                if (callBack != null) {
                    callBack.callback(false, inflate);
                }
                CWLog.e("lzh ImageLoader: ", " onLoadingCancelled " + s);

            }
        });
    }

    protected static int dp2px(Context context, int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
    }
}
